/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Clazz;
import me.zhengjie.modules.system.domain.Clazzanddepartment;
import me.zhengjie.modules.system.domain.vo.ClazzanddepartmentQueryCriteria;
import me.zhengjie.modules.system.service.ClazzService;
import me.zhengjie.modules.system.service.ClazzanddepartmentService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* @author shitou
* @date 2025-02-02
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "班级部门管理")
@RequestMapping("/api/clazzanddepartment")
public class ClazzanddepartmentController {

    private final ClazzanddepartmentService clazzanddepartmentService;

    private final ClazzService clazzService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('clazzanddepartment:list')")
    public void exportClazzanddepartment(HttpServletResponse response, ClazzanddepartmentQueryCriteria criteria) throws IOException {
        clazzanddepartmentService.download(clazzanddepartmentService.queryAll(criteria), response);
    }

    @GetMapping("/queryClazzanddepartment")
    @Log("查询班级部门")
    @ApiOperation("查询班级部门")
//    @PreAuthorize("@el.check('clazzanddepartment:list')")
    public ResponseEntity<List<Map<String,Object>>> queryClazzanddepartment(ClazzanddepartmentQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(clazzanddepartmentService.queryClazzanddepartment(criteria),HttpStatus.OK);
    }

    @GetMapping("/queryTeacherClazz")
    @Log("查询班级部门")
    @ApiOperation("查询班级部门")
//    @PreAuthorize("@el.check('clazzanddepartment:list')")
    public ResponseEntity<List<Map<String,Object>>> queryTeacherClazz(ClazzanddepartmentQueryCriteria criteria){
        return new ResponseEntity<>(clazzanddepartmentService.queryTeacherClazz(criteria),HttpStatus.OK);
    }

    @GetMapping("/queryClazzOtherNum")
    @Log("查询班级部门")
    @ApiOperation("查询班级部门")
//    @PreAuthorize("@el.check('clazzanddepartment:list')")
    public ResponseEntity<Map<String,Object>> queryClazzOtherNum(ClazzanddepartmentQueryCriteria criteria){
        return new ResponseEntity<>(clazzanddepartmentService.queryClazzOtherNum(criteria),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增班级部门")
    @ApiOperation("新增班级部门")
//    @PreAuthorize("@el.check('clazzanddepartment:add')")
    public ResponseEntity<Object> createClazzanddepartment(@Validated @RequestBody Clazz resources){

        clazzanddepartmentService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改班级部门")
    @ApiOperation("修改班级部门")
//    @PreAuthorize("@el.check('clazzanddepartment:edit')")
    public ResponseEntity<Object> updateClazzanddepartment(@Validated @RequestBody Clazz resources){
        clazzanddepartmentService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除班级部门")
    @ApiOperation("删除班级部门")
//    @PreAuthorize("@el.check('clazzanddepartment:del')")
    public ResponseEntity<Object> deleteClazzanddepartment(@RequestBody List<Integer> ids) {
        System.out.println("==ids=="+ids);
//        clazzanddepartmentService.deleteAll(ids);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.in("clazzId",ids);
        clazzanddepartmentService.remove(queryWrapper);

        QueryWrapper queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("id",ids);
        clazzService.remove(queryWrapper2);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}